import { mapState } from 'vuex'
import commonJs from '~/mixins/commonJs'
export default {
  computed: {
    ...mapState({
      locale: (state) => state.locale,
    }),
  },
  mixins: [commonJs],
  data() {
    return {
      throttledClickFunctions: {},
    }
  },
  methods: {
    /**
     *
     * @param {obj} toolsData 工具
     * @param {string} toolPosition 页面中的位置
     * @param {string} toolPosIndex 列表中的工具序号下标
     * @param {boolean} [isSearch=false] 是否搜索页的搜索列表
     * @param {{}} [prePropertyObj={}] 预置提交的属性
     * @returns
     */
    getExporeData(
      toolsData,
      toolPosition,
      toolPosIndex,
      isSearch = false,
      prePropertyObj = {}
    ) {
      let page = this.getCurPageName()

      // 页面位置
      let toolPositionRes = ''
      if (prePropertyObj.page_position) {
        toolPositionRes = `#${prePropertyObj.page_position}`
      } else {
        toolPositionRes = toolPosition
        if (isSearch) {
          toolPositionRes = 'Search list'
        }
        let index = String(toolPosIndex + 1).padStart(2, '0')
        toolPositionRes = `#${toolPositionRes} - ${index}`
      }

      let browserName = this.getBroswer()
      let deviceType = this.getDeviceType()

      const userLanguage = navigator.language || navigator.userLanguage

      let target_url = ''
      if (prePropertyObj.target_url) {
        target_url = prePropertyObj.target_url
      } else {
        target_url =
          (this.hasRealVal(toolsData.is_recommend_now) &&
            toolsData.is_recommend_now) ||
          this.hasRealVal(toolsData.advertisement_id)
            ? toolsData.website
            : `https://www.toolify.ai${
                this.locale === 'en' ? '' : `/${this.locale}`
              }/tool/${toolsData.handle}`
      }
      return {
        page_name: page,
        page_position: toolPositionRes,
        url: window.location.href,
        target_url: target_url,
        tool_handle: toolsData.handle,
        referer: document.referrer || null,
        page_language: this.locale,
        browser_language: userLanguage,
        browser: browserName,
        device_type: deviceType,
        advertisement_id: this.hasRealVal(toolsData.advertisement_id)
          ? toolsData.advertisement_id
          : null,
      }
    },
  },
}
